package Class_Object.接口.应用实例之可比较;

import java.util.Arrays;
import java.util.Comparator;

//TODO 引用类型来作比较
class Student implements Comparable<Student>{//实现了该接口,表示当前类是可以进行比较的
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
//        System.out.println("正在比较");
//        if(this.age > o.age) {
//            return 1;
//        } else if (this.age == o.age) {
//            return 0;
//        }else {
//            return -1;//TODO 实现了Compareable接口,重写了compareTo方法
            //但是根据姓名比较呢?要大改所以不合适
        }
    }
//}
//class Student {
//    public String name;
//    public int age;
//
//    public Student(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }

//    @Override
//    public int compareTo(Student o) {
//        if(this.age > o.age) {
//            return 1;
//        } else if (this.age == o.age) {
//            return 0;
//        }else {
//            return -1;//TODO 实现了Compareable接口,重写了compareTo方法
//            //但是根据姓名比较呢?要大改所以不合适
//        }
//    }
//}
//TODO 我们可以用比较器来解决上述问题
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void bubbleSort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable temp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = temp;
                }
            }
        }

    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zs",15);
        students[1] = new Student("ls",13);
        students[2] = new Student("wxw",14);
        System.out.println("排序前=========");
        System.out.println(Arrays.toString(students));
        bubbleSort(students);
        System.out.println("排序后=========");
        System.out.println(Arrays.toString(students));

    }
    public static void main4(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zs",12);
        students[1] = new Student("ls",13);
        students[2] = new Student("wxw",14);
        System.out.println("排序前=========");
        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,ageComparator);//TODO 直接用比较器比较
        System.out.println("年龄比较后: "+Arrays.toString(students));

        Arrays.sort(students,nameComparator);//传入比较器,更加灵活
        System.out.println("姓名比较后: "+Arrays.toString(students));
        //排序要规定用什么排序Arrays.sort会调用Comparable方法,所以我们要自己重写一下
//        System.out.println("排序后=========");
//        Arrays.sort(students);//这里面的compareto调用的是我们自己重写的,但是比较死板,写死了就不能动了

//        System.out.println(Arrays.toString(students));



    }
    public static void main2(String[] args) {
        Student student1 = new Student("张三",12);
        Student student2 = new Student("李三",12);
        System.out.println(student2==student1);
        //如何比较这俩个对象呢?根据什么比较?年龄?姓名?
//        System.out.println(student1.compareTo(student2));
//        AgeComparator ageComparator = new AgeComparator();
//        System.out.println(ageComparator.compare(student1, student2));//调用的是String自己重写的compareTo方法
//        NameComparator nameComparator = new NameComparator();
//        System.out.println(nameComparator.compare(student2, student1));
    }
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a > b);
    }
}
